package com.axt.manager.unitTest;

import java.util.List;

import com.axt.manager.bean.AuthBean;
import com.axt.manager.bean.LoginBean;
import com.axt.manager.bean.NodeBean;
import com.axt.manager.bean.UserBean;
import com.axt.manager.model.Auth;
import com.axt.manager.model.Node;
import com.axt.util.Md5;

public class SoaManager {
	LoginBean loginBean;
	NodeBean nodeBean;
	UserBean userBean;
	AuthBean authBean;
	String szSesKey = "";
	int nUserId = 1;
	long nTimeStap = 0;

	void test() {
		login();
		//addNode();
//		addAuthority();
		// modifyNode();
		getServers();
		// addAuthority();
	}

	// user login
	void login() {
		loginBean = new LoginBean();
		int nRetVal = -1;
		String passwd = Md5.getMd5("bjaxt");
		long timestap = loginBean.queryTimestamp();
		passwd = Md5.getMd5("superadmin"+passwd+timestap);
		nRetVal = loginBean.login("superadmin", passwd, timestap);
		if (nRetVal == -1)
			System.out.println("login fail");

		System.out.println("user id=" + loginBean.getUserId());
		System.out.println("role id=" + loginBean.getRoleId());
		System.out.println("session key=" + loginBean.getSesKey());
	}

	// 获取serverId
	int getServerId(String szServerAddr, String port) {
		int nServerId = -1;
		nServerId = new NodeBean().queryServerId(szServerAddr, port);
		if (nServerId == -1)
			System.out.println("get server id fail");
		else
			System.out.println("server id:" + nServerId);
		
		return nServerId;
	}

	// 增加节点
	void addNode() {
		int nRetVal = -1;
		nodeBean = new NodeBean(); 
		nRetVal = nodeBean.addNode("新增测试节点", "3", "192.168.1.200",
		        "9000");
		if (nRetVal != -1)
			System.out.println("add node success, node id:"
			        + nRetVal);
		else
			System.out.println("add node fail");
	}

	void deleteNode() {
		int nRetVal = -1;
		nRetVal = nodeBean.deleteNode("19");
		if (nRetVal != -1)
			System.out.println("delete node success, delete node id:19");
		else
			System.out.println("delete node fail");
	}

	void modifyNode() {
		int nRetVal = -1;
		nRetVal = nodeBean.modifyNode("25", "test1", "3",
		        "192.168.1.212", "8000");
		if (nRetVal == -1)
			System.out.println("修改失败");
		else
			System.out.println("修改成功");

	}

	void getServers() {
		List<Node> nodes = new NodeBean().queryNodeList(1);
		if (nodes != null) {
			if (nodes.size() != 0) {
				for (int i = 0; i < nodes.size(); i++) {
					System.out.println("server id:"
					        + nodes.get(i).getServerId() + " server name："
					        + nodes.get(i).getServerName() + "server type:"
					        + nodes.get(i).getServerType());
				}
			}
		} else
			System.out.println("获取失败");
	}

	int addAuthority() {
		int nRet = authBean.addAuth("2", "12", "1");
		if (0 == nRet) {
			System.out.println("添加权限成功!");
			return 0;
		} else {
			System.out.println("添加权限失败!");
			return -1;
		}
	}

	int deleteAuthority() {
		int nRet = authBean.deleteAuth("4", "13");
		if (0 == nRet) {
			System.out.println("删除权限成功!");
			return 0;
		} else {
			System.out.println("删除权限失败!");
			return -1;
		}
	}

	public static void main(String[] args) throws Exception {
		new SoaManager().test();
	}
}
